/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simulator;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import simulator.network.Manager;
import simulator.network.Network;
import simulator.network.Output;

/**
 *
 * @author Pascal Schwenke <pascal.schwenke@gmail.com>
 */
public class Simulator {

    static boolean exitApplication = false;
    static JButton b;
    static JPanel p;
    static JLabel picLabel;
    static JLabel l;
    static JSplitPane split;
    
    static Manager manager;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        manager = new Manager();

        //System.out.println(Output.NetworkCellTypesToString(manager.GetNetwork()));
        System.out.println(Output.NetworkUserCountToString(manager.GetNetwork()));
        //System.out.println(Output.NetworkNeighborCountToString(manager.GetNetwork()));
        //System.out.println(Output.NetworkToString(manager.GetNetwork()));
        //manager.CreateRules(6);
/*
         try {
         Output.NetworkToHeatmap(manager.GetNetwork(), "FirstHeatmap", "firstHeatmap.png");
         } catch (IOException ex) {
         Logger.getLogger(Simulator.class.getName()).log(Level.SEVERE, null, ex);
         }

         //ArrayList<Network> nets=new ArrayList<>());
         for (int i = 0; i < 100; i++) {

         Network nextStep = manager.SimulationStep();
         manager.SetNetwork(nextStep);
         System.out.println(Output.NetworkUserCountToString(nextStep));
         try {
         Output.NetworkToHeatmap(manager.GetNetwork(), "Heatmap", "Heatmap" + i + ".png");
         } catch (IOException ex) {
         Logger.getLogger(Simulator.class.getName()).log(Level.SEVERE, null, ex);
         }
         }
         */
        JFrame f = new JFrame("Simulator");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(800, 400);

        BufferedImage myPicture = ImageIO.read(new File("C:\\Users\\Pascal\\Documents\\NetBeansProjects\\Simulator\\firstHeatmap.png"));
        JLabel picLabel = new JLabel(new ImageIcon(myPicture));

        p = new JPanel();

        l = new JLabel(manager.GetDayTime());
        b = new JButton("Change time of day");
        b.addActionListener(new ButtonClick());

        p.add(l);
        p.add(b);

        split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, picLabel, p);

        f.add(split);
        f.setVisible(true);

        for (int i = 0; i < 100; i++) {

            Network nextStep = manager.SimulationStep();
            manager.SetNetwork(nextStep);
            System.out.println(Output.NetworkUserCountToString(nextStep));
            try {
                Output.NetworkToHeatmap(manager.GetNetwork(), "Heatmap t="+i, "Heatmap" + i + ".png");
            } catch (IOException ex) {
                Logger.getLogger(Simulator.class.getName()).log(Level.SEVERE, null, ex);
            }
            BufferedImage nextPic = ImageIO.read(new File("C:\\Users\\Pascal\\Documents\\NetBeansProjects\\Simulator\\Heatmap" + String.valueOf(i) + ".png"));
            picLabel.setIcon(new ImageIcon(nextPic));
        }


        //System.out.println(Output.TestTransitionRules(manager.GetNetwork()));
    }

    static class ButtonClick implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == b) {
                manager.ChangeDayTime();
                l.setText(manager.GetDayTime());
            }
        }
    }
}
